package com.shumi.rbac.web;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.shumi.rbac.model.User;


@RestController
/*@RequestMapping(value="${adminPath}/platform/upload")*/
@RequestMapping(value="/upload")
public class UploadController {
	
	@Autowired  
    private HttpServletRequest request;
	/*@Autowired 
	private User user;*/
	/*@RequestParam(value = "fileList", required = false) MultipartFile[] fileList*/
	@ResponseBody
	@RequestMapping(value = "upload")
	public String upload(@RequestParam(value = "fileList", required = false) CommonsMultipartFile[] fileList){
		String lrr ="1";
		String relativePath = "static\\common\\upload" +"\\"+lrr+"\\"+new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		String fileBasePath = request.getSession().getServletContext().getRealPath("/") + relativePath;
		System.out.println("图片上传路径："+fileBasePath);
		StringBuffer resultName = new StringBuffer();
		try {
			File fileDir = new File(fileBasePath);
			if (!fileDir.exists()) {
				fileDir.mkdirs();
			}
			for (CommonsMultipartFile multipart : fileList) {
				System.out.println(multipart.getOriginalFilename());
				String newFileNamesString = generateFileName(multipart.getOriginalFilename());
				FileOutputStream out = new FileOutputStream(new File(fileBasePath,newFileNamesString));
				// 写入文件
				out.write(multipart.getBytes());
				out.flush();
				out.close();
				resultName.append(relativePath+"\\"+newFileNamesString+",");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		String str = resultName.toString();
		return str.substring(0,str.length() - 1).replace("\\", "/");
	}
	
	private static String generateFileName(String fileName){
		String dataFormat = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		int random = new Random().nextInt(10000);
		String ext = fileName.substring(fileName.lastIndexOf("."));
		String newFileName = dataFormat+random+ext;
		return newFileName;
	}
	
	public Map<String, Object> deleteFile(String imgPath){
		Map<String, Object> map = new HashMap<String, Object>();
		String fileBasePath = request.getSession().getServletContext().getRealPath("/");
		List<String> successList = new ArrayList<String>();
		List<String> failList = new ArrayList<String>();
		if(imgPath.indexOf(",") == -1){
			File file = new File(fileBasePath+imgPath);
			if(file.exists()){
				if(file.isFile()){
					if(file.delete()){
						successList.add("删除【"+imgPath+"】文件成功！");
					}else{
						failList.add("删除文件失败：【"+imgPath+"】被占用！");
					}
				}else{
					failList.add("删除文件失败，不能删除文件夹!");
				}
			}else{
				failList.add("删除文件失败：【"+imgPath+"】不存在！");
			}
		}else{
			String[]  imgurls = imgPath.split(",");
			File file ;
			for (String imgUrl : imgurls) {
				file = new File(fileBasePath + imgUrl);
				if(file.exists()){
					if(file.isFile()){
						if(file.delete()){
							successList.add("删除【"+imgUrl+"】文件成功!");
						}else{
							failList.add("删除文件失败：【"+imgUrl+"】被占用！");
						}
					}else{
						failList.add("删除文件失败，不能删除文件夹!");
					}
				}else{
					failList.add("删除文件失败：【"+imgUrl+"】不存在!");
				}
			}
		}
		map.put("success", successList);
		map.put("fail", failList);
		return map;
	}
	
	@ResponseBody
	@RequestMapping(value = "delete")
	public Map<String, Object> delete(String imgPath){
		Map<String, Object> map = new HashMap<String, Object>();
		String fileBasePath = request.getSession().getServletContext().getRealPath("/");
		File file = new File(fileBasePath+imgPath);
		String result = "fail";
		if(file.exists() && file.isFile()){
			if(file.delete()){
				result = "success";
			}else{
			}
		}
		map.put("data", result);
		return map;
	}
	

}
